<?php
include_once("sessionmanager.php");
initSession();

if (!isset($_SESSION['s_loggedin']) || ($_SESSION['s_loggedin'] == false) || !isset($_SESSION['s_serverurl']) || (strpos($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],$_SESSION['s_serverurl']) === false)) {
	$error_page_code = "timeout";
	include "errorpage.php";
	exit();
}
if (!$_SESSION['s_cansearch']) {
	$error_page_code = "permission";
	include "errorpage.php";
	exit();
}
include "languages.php";
include_once "functions.inc.php";
include "attributes.php";
$userid = $_SESSION['s_userid'];

makeheader(VIEW_TITLE);

if (isset($_GET['gala']) && isset($_GET['system']) && is_numeric($_GET['gala']) && is_numeric($_GET['system'])) {
	$gala   = $_GET['gala'];
	$system = $_GET['system'];
	if ($gala < 1) $gala = 1;
	if ($system < 1) $system = 1;
	if ($system > 499) $system = 499;
	unset($_SESSION['s_gv_galaxy']);
	unset($_SESSION['s_gv_system']);
	$_SESSION['s_gv_galaxy'] = $gala;
	$_SESSION['s_gv_system'] = $system;

	require "config.php";
	mysql_connect($dbhost,$dbusername,$dbpassword) or die(PARSER_ERROR3);
	mysql_select_db($dbname) or die(PARSER_ERROR2);
	$query = "SELECT u.*, p.rank, p.points, p.frank, p.fpoints, p.rrank, p.rpoints, a.rank as arank, p.last_update as plastupdate,
	a.members, a.allystatus, a.rank as arank, a.points as apoints, a.frank as afrank, a.fpoints as afpoints, a.rrank as arrank, a.rpoints as arpoints,
	a.last_update as alastupdate, r.*, p.status, p.ally as pally
	FROM $dbtablename u LEFT JOIN $pstablename p ON (u.userid=p.userid and u.spielername=p.playername)
	LEFT JOIN $astablename a ON (u.userid=a.userid and u.ally=a.allyname)
	LEFT JOIN $reporttable r ON (u.userid=r.userid and u.coordinates = r.coordinates)
	WHERE u.userid=$userid and u.galaxie='$gala' AND u.system='$system' ORDER BY planet ASC,r.moon DESC";
	$res = mysql_query($query);
	?>
<script type="text/javascript">
function galadown () {
	document.viewform.gala.value = <?php echo $gala-1; ?>;
	document.viewform.submit();
}
function galaup () {
	document.viewform.gala.value = <?php echo $gala+1; ?>;
	document.viewform.submit();
}
function systemdown () {
	document.viewform.system.value = <?php echo $system-1; ?>;
	document.viewform.submit();
}
function systemup () {
	document.viewform.system.value = <?php echo $system+1; ?>;
	document.viewform.submit();
}
</script>
<form action="view.php" name="viewform">
<table cellpadding="0" cellspacing="0" border="0" align="center" width="85%">
<tr class="tblhead">
<td align="right" style="padding-right:20px;"><?php echo VIEW_GALAXY; ?></td><td style="width:30px;">&nbsp;</td><td align="left" style="padding-left:20px;"><?php echo VIEW_SYSTEM; ?></td></tr>
<tr class="firstcolor">
<td align="right">
<input class="button" type="button" name="gback" value="&lt;" onclick="javascript:galadown()" />
<input class="textfield" style="text-align:center;" type="text" name="gala" value="<?php echo $gala; ?>" maxlength="2" size="2" />
<input class="button" type="button" name="gfwd" value="&gt;" onclick="javascript:galaup()" /></td>
<td style="width:30px;"><input class="button" type="submit" name="go" value="go" /></td>
<td align="left">
<input class="button" type="button" name="sback" value="&lt;" onclick="javascript:systemdown()" />
<input class="textfield" style="text-align:center;" type="text" name="system" value="<?php echo $system; ?>" maxlength="3" size="3" />
<input class="button" type="button" name="sfwd" value="&gt;" onclick="javascript:systemup()" />
</td></tr>
</table>
</form>
<table cellpadding="0" cellspacing="2" border="0" align="center" width="85%">
	<?php

	if (!empty($ostat_universe) && is_numeric($ostat_universe) && !empty($ostat_server) && (
	$ostat_server == "de" || $ostat_server == "org")) {
		$ostat = true;
	} else $ostat = false;

	if (mysql_num_rows($res) == 0) {
		echo "<tr class=\"tblhead\"><td colspan=\"8\">".VIEW_SYSTEM." $gala:$system</td></tr>\n";
		echo "<tr class=\"firstcolor\"><td colspan=\"8\">".VIEW_NODATA."</td></tr>\n";
	} else {
		$i = 0;
		$planets = 0;
		$old_coordinates = "";
		$spios = 1;
		while ($line = mysql_fetch_object($res)) {
			if ($i == 0) {
				echo "<tr class=\"tblhead\"><td colspan=\"8\">".VIEW_SYSTEM." $gala:$system @ ".date("d. M. Y",$line->zeit)."</td></tr>\n";
				echo "<tr class=\"tblhead\"><td>".VIEW_POSITION."</td><td>".VIEW_PLANET."</td><td>".VIEW_MOON."</td><td>".VIEW_TF."</td><td style=\"font-size:7pt;\">".VIEW_PLAYER." <span>".VIEW_PLAYER_ADD."</span></td><td>".VIEW_ALLY."<br /><span style=\"font-size:7pt;\">".VIEW_ALLY_ADD."</span></td><td>".VIEW_NOTICE."</td><td>".VIEW_REPORTS."</td></tr>\n";
			} else {
				if ($old_coordinates != $line->galaxie.":".$line->system.":".$line->planet)
				echo "</td></tr>\n";
			}

			if ($old_coordinates != $line->galaxie.":".$line->system.":".$line->planet) {
				echo "<tr class=\"firstcolor\" align=\"center\">\n";
				//Position
				echo "<td style=\"width:30px;\" align=\"center\">".$line->planet."</td>\n";
				// Planetname
				echo "<td align=\"center\">".$line->planetname."</td>\n";
				// Mond
				if ($line->mond == 1) {
					echo "<td align=\"center\">M<br><span style=\"font-size:6pt;\">";
					if ($line->mondgr > 0) echo "(".number_format($line->mondgr,0,",",".")." km)";
					else echo VIEW_UKMOONSIZE;
					echo "</span></td>\n";
				}
				else echo "<td>&nbsp;</td>\n";
				// TF
				if ($line->metall > 0 || $line->kristall > 0) echo "<td style=\"font-size:6pt;\" align=\"center\">".VIEW_METAL.":".number_format($line->metall,0,",",".")."<br>".VIEW_CRYSTAL.":".number_format($line->kristall,0,",",".")."</td>\n";
				else echo "<td>&nbsp;</td>\n";
				// Spielername
				echo "<td align=\"center\"><table width=\"100%\"><tr class=\"firstcolor\"><td align=\"center\">";
				if (!empty($line->spielername)) {
					$spielerstatus = $line->status;

					// show playername and all playerstats
					echo "<a class=\"link\" href=\"show.php?spieler=".urlencode($line->spielername)."&new=true\"";

					//if (!empty($line->rank)) {
					if (isset($line->rank) && $line->rank != -1) {
						echo ' onmouseover=\'this.T_WIDTH=250;return escape("'.htmlentities('<div style=font-size:10pt;>'.STATS_PLAYERSTATS.":</div><br>".SHOW_RANK.": #".$line->rank." - ".STATS_SCORE.": ".number_format($line->points,0,",",".")."<br>".STATS_FLEET.": #".$line->frank." - ".STATS_SCORE.": ".number_format($line->fpoints,0,",",".")."<br>".STATS_RESEARCH.": #".$line->rrank." - ".STATS_SCORE.": ".number_format($line->rpoints,0,",","."))."<br><br>".STATS_UPDATE.": ".$line->plastupdate.'")\' >';
					} else {
						echo ' onmouseover=\'this.T_WIDTH=250;return escape("'.htmlentities(SHOW_NOTRANKED).'")\' >';
					}


					// Status
					$openspans = 0;
					if (strpos($spielerstatus,"g") !== false) {
						echo "<span class=\"banned\">";
						$openspans++;
					}
					if (strpos($spielerstatus,"u") !== false) {
						echo "<span class=\"vacation\">";
						$openspans++;
					} 
					if (strpos($spielerstatus,"n") !== false) {
						echo "<span class=\"noob\">";
						$openspans++;
					} 
					if (strpos($spielerstatus,"i") !== false) {
						echo "<span class=\"longinactive\">";
						$openspans++;
					} 
					if (strpos($spielerstatus,"I") !== false) {
						echo "<span class=\"inactive\">";
						$openspans++;
					}
					if (strpos($spielerstatus,"s") !== false) {
						echo "<span class=\"superman\">";
						$openspans++;
					} 					
					// print player with stats color
					echo $line->spielername;
					for ($i=$openspans;$i>0;$i--) {
						echo "</span>";
					}
					echo "</a>";

					if (!empty($line->status) || (isset($line->rank) && $line->rank != -1)) {
						echo " ( ";
						if(isset($line->rank) && $line->rank != -1){
							echo "<span style=\"color:snow;\">" . $line->rank . "</span>";
							if(!empty($line->status)){
								echo " / ";
							}
						}
					    if(!empty($line->status)){
						$spielerstatus = $line->status;
						
						switch ($_SESSION['lang']) {
							case "english" :
								print_status(false,$spielerstatus,"b","v"); // banned, vacation, noob, inactive, long inactive
								break;
							default:
								print_status(false,$spielerstatus);
								break;
						}
					    }
						echo " )";
					}

				}
				echo "</td>\n";
				// Ostat
				if ($ostat && !empty($line->spielername)) echo "<td align=\"right\"><a target=\"ostat\" href=\"http://uni$ostat_universe.ostat.$ostat_server/index.php?ext=player&amp;name=".urlencode($line->spielername)."\"><img src=\"../images/os.gif\" border=\"0\"></a></td>";
				else echo "<td>&nbsp;</td>";
				echo "</tr></table>";
				// Allyname
				echo "<td align=\"center\"><table width=\"100%\"><tr class=\"firstcolor\"><td align=\"center\"><a class=\"link\" href=\"show.php?ally=".urlencode($line->ally)."&new=true\">";
				if (!empty($line->ally)) {
					echo "<span class=\"".$line->allystatus."\" ";
					if (!empty($line->arank)) {
						echo ' onmouseover=\'this.T_WIDTH=250;return escape("'.htmlentities('<div style=font-size:10pt;>'.STATS_ALLYSTATS.":</div><br>".STATS_MEMBERS.": ".$line->members."<br>".SHOW_RANK.": #".$line->arank." - ".STATS_SCORE.": ".number_format($line->apoints,0,",",".")."<br>".STATS_FLEET.": #".$line->afrank." - ".STATS_SCORE.": ".number_format($line->afpoints,0,",",".")."<br>".STATS_RESEARCH.": #".$line->arrank." - ".STATS_SCORE.": ".number_format($line->arpoints,0,",","."))."<br><br>".STATS_UPDATE.": ".$line->plastupdate.'")\' >';
					} else {
						echo ' onmouseover=\'this.T_WIDTH=250;return escape("'.htmlentities(SHOW_NOTRANKED).'")\' >';
					}

					echo $line->ally;
					echo "</span>";
				}
				if (!empty($line->members)) echo " (".$line->arank." / ".$line->members.")";
				echo "</a></td>";
				// Ostat
				if ($ostat && !empty($line->ally)) echo "<td align=\"right\"><a target=\"ostat\" href=\"http://uni$ostat_universe.ostat.$ostat_server/index.php?ext=allyueber&amp;name=".urlencode($line->ally)."\"><img src=\"../images/os.gif\" border=\"0\"></a></td>";
				else echo "<td>&nbsp;</td>";
				echo "</tr></table></td>\n";

				// Notizblock
				if ($line->notices == "true")
				echo '<td><a href="notices.php?coordinates='."$gala:$system:".$line->planet.'&amp;action=view"><img src="../images/notice.gif" border="0"></a></td>';
				else
				echo '<td><a href="notices.php?coordinates='."$gala:$system:".$line->planet.'&amp;action=edit"><img src="../images/no_notice.gif" border="0"></a></td>';

				// Spionage
				if (!empty($line->coordinates)) {
					$moon = ($line->moon == "false") ? "" : "&amp;moon=1";
					echo '<td><a href="reports.php?coordinates='."$gala:$system:".$line->planet.$moon.'"><img src="../images/spio.gif" border="0"></a>';
				} else {
					echo '<td><img src="../images/no_spio.gif" border="0">';
				}
				$old_coordinates = $line->galaxie.":".$line->system.":".$line->planet;
				if (!empty($line->spielername)) $planets++;
			} else {
				if (!empty($line->coordinates)) {
					$moon = ($line->moon == "false") ? "" : "&amp;moon=1";
					echo '&nbsp;<a href="reports.php?coordinates='."$gala:$system:".$line->planet.$moon.'"><img src="../images/spio.gif" border="0"></a>';
				} else {
					echo '&nbsp;<img src="../images/no_spio.gif" border="0">';
				}

			}
			$i++;
			if (!isset($galaxie)) $galaxie = $line->galaxie;
			if (!isset($system)) $system = $line->system;
		}
		echo "</td></tr>\n";
		echo "<tr class=\"tblhead\"><td colspan=\"8\">($planets ".VIEW_COLONISED.")</td></tr>\n";
		echo "<tr class=\"firstcolor\"><td align=\"center\" colspan=\"8\" style=\"white-space: nowrap;\">";
		// Colors
    	?>
    	<span class="bg_nothing">&nbsp;&nbsp;</span> <?php echo STATS_NOTHING; ?>&nbsp;&nbsp;&nbsp;&nbsp;
    	<span class="bg_neutral">&nbsp;&nbsp;</span> <?php echo STATS_NEUTRAL; ?>&nbsp;&nbsp;&nbsp;&nbsp;
    	<span class="bg_boycott">&nbsp;&nbsp;</span> <?php echo STATS_BOYCOTT; ?>&nbsp;&nbsp;&nbsp;&nbsp;
    	<span class="bg_nap">&nbsp;&nbsp;</span> <?php echo STATS_NAP; ?>&nbsp;&nbsp;&nbsp;&nbsp;
    	<span class="bg_ally">&nbsp;&nbsp;</span> <?php echo STATS_ALLIED; ?>&nbsp;&nbsp;&nbsp;&nbsp;
    	<span class="bg_war">&nbsp;&nbsp;</span> <?php echo STATS_WAR; ?>&nbsp;&nbsp;&nbsp;&nbsp;
    	<span class="bg_own">&nbsp;&nbsp;</span> <?php echo STATS_OWN; ?>&nbsp;&nbsp;&nbsp;&nbsp;
    	<span class="bg_wing">&nbsp;&nbsp;</span> <?php echo STATS_WING; ?>&nbsp;&nbsp;&nbsp;&nbsp;
    	<?php
    	// Phalanx
    	echo "</td></tr>\n";
    	echo "<tr class=\"tblhead\"><td colspan=\"8\">".VIEW_MOONPHALANX."</td></tr>\n";
    	echo "<tr class=\"firstcolor\"><td align=\"center\" colspan=\"8\">\n";
    	$query = "SELECT g.coordinates,g.system,g.galaxie,a.allystatus
				  FROM $reporttable r LEFT JOIN  $dbtablename g on  (r.userid=g.userid and r.coordinates=g.coordinates) LEFT JOIN $astablename a ON (g.userid=a.userid and g.ally=a.allyname)
				  WHERE r.userid=$userid and r.moon='true' AND r.sensor>0 AND g.galaxie='$galaxie' AND (
				  '$system' between (g.system - ((r.sensor*r.sensor)-1)) AND (g.system + (r.sensor*r.sensor)-1))
				  ORDER BY g.coordinates";
    	$res = mysql_query($query) or die(mysql_error());
    	if (mysql_num_rows($res) == 0) {
    		echo VIEW_NOMOONS;
    	} else {
    		$i = 1;
    		while ($line = mysql_fetch_object($res)) {
    			echo "<a class=\"link\" href=\"view.php?gala=$line->galaxie&amp;system=$line->system\"><span class=\"".$line->allystatus."\">".$line->coordinates."</span></a>&nbsp;&nbsp;&nbsp;";
    			if ($i % 10 == 0) {
    				echo "<br />\n";
    			}
    			$i++;
    		}
    	}
   		echo "</td></tr>\n";
   	
   		// Iraks
    	echo "</td></tr>\n";
    	echo "<tr class=\"tblhead\"><td colspan=\"8\">".VIEW_IRAKS."</td></tr>\n";
    	echo "<tr class=\"firstcolor\"><td align=\"center\" colspan=\"8\">\n";
    	$query = "SELECT *
				  FROM $reporttable r left join  $dbtablename g on  (r.userid=g.userid and r.coordinates=g.coordinates)  LEFT JOIN $astablename a ON (g.userid=a.userid and g.ally=a.allyname) 
				  WHERE  r.userid=$userid and r.raksilo>3 AND g.galaxie='$galaxie' AND (
				  '$system' between (g.system - ((r.impulse *2)-1)) AND (g.system + (r.impulse *2)-1))
				  ORDER BY g.coordinates";
    	$res = mysql_query($query) or die(mysql_error());
    	if (mysql_num_rows($res) == 0) {
    		echo VIEW_NOIRAKS;
    	} else {
    		$i = 1;
    		while ($line = mysql_fetch_object($res)) {
    			echo "<a class=\"link\" href=\"view.php?gala=$line->galaxie&amp;system=$line->system\"><span class=\"".$line->allystatus."\">".$line->coordinates."</span></a>&nbsp;&nbsp;&nbsp;";
    			if ($i % 10 == 0) {
    				echo "<br />\n";
    			}
    			$i++;
    		}
    	}
   	echo "</td></tr>\n";

   	

   	
   	
    	if (isset($_GET['from']) && $_GET['from'] == "show")
    	echo "<tr class=\"tblhead\"><td colspan=\"8\"><a class=\"link\" href=\"javascript:history.back();\">".VIEW_BACK."</a></td></tr>\n";
	}
}

?>
</table>
<?php
makefooter();
?>